package dao;

import Utils.JDBCUtil;
import pojo.Course;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CourseDao {
    public Course findOne(String id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Course course = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from course where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                course = new Course(id, resultSet.getString("name"), resultSet.getString("title"),
                        resultSet.getString("difficulty"), resultSet.getInt("time"),
                        resultSet.getInt("price"), resultSet.getString("text"),resultSet.getString("imagepath"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(resultSet, preparedStatement, connection);
        }
        return course;
    }
}
